using System;
using System.Collections.Generic;
using System.Text;
using BizTalk.ESB.Extensions.Helpers;

namespace BizTalk.Core.Mapping
{
    public class MappingTools
    {
        #region Logic Helpers

        public static Boolean NodeHasValue(String NodeValue, Boolean Debug)
        {
            return (!String.IsNullOrEmpty(NodeValue));
        }

        #endregion

        #region Date Formatting

        public String FormatDateString(String StringDate, String CurrentDateFormat, String NewDateFormat)
        {
            DateTime parsedStringDate = DateTime.ParseExact(StringDate, CurrentDateFormat
                , System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None);
            String formattedDate = parsedStringDate.ToString(NewDateFormat);
            return formattedDate;
        }
        
        public static String CreateFormattedDate(String Day, String Month, String Year, String DateFormat, Boolean Debug)
        {
            if (String.IsNullOrEmpty(Day) || String.IsNullOrEmpty(Month) || String.IsNullOrEmpty(Year))
            {
                String ErrorMessage = "Day, Month and Year values must not be null or empty";
                System.Diagnostics.Trace.WriteLineIf(Debug,
                    DebugMessageFactory.GenerateTraceMessage(
                        System.Reflection.Assembly.GetExecutingAssembly().FullName,
                        System.Reflection.Assembly.GetExecutingAssembly().HostContext.ToString(),
                        ErrorMessage,
                        System.Diagnostics.EventLogEntryType.Error));
                throw new ArgumentException(ErrorMessage);
            }
            else
            {
                DateTime dt = new DateTime(
                    System.Convert.ToInt32(Year),
                    System.Convert.ToInt32(Month),
                    System.Convert.ToInt32(Day)
                    );
                return dt.ToString(DateFormat);
            }
        }

        public static String ReformatXmlDate(String XmlDate, String DateFormat, Boolean Debug)
        {
            if (String.IsNullOrEmpty(XmlDate) || String.IsNullOrEmpty(DateFormat))
            {
                String ErrorMessage = "XmlDate and DateFormat values must not be null or empty";
                System.Diagnostics.Trace.WriteLineIf(Debug,
                    DebugMessageFactory.GenerateTraceMessage(
                        System.Reflection.Assembly.GetExecutingAssembly().FullName,
                        System.Reflection.Assembly.GetExecutingAssembly().HostContext.ToString(),
                        ErrorMessage,
                        System.Diagnostics.EventLogEntryType.Error));
                return String.Empty;
            }
            else
            {
                try
                {

                    DateTime dt = new DateTime();
                    dt = System.Convert.ToDateTime(XmlDate);
                    return dt.ToString(DateFormat);
                }
                catch (Exception e)
                {
                    System.Diagnostics.Trace.WriteLine("Date Format Failed: Date = " + XmlDate + ", Format = " + DateFormat);
                    return String.Empty;
                }
            }
        }

        #endregion

        #region Name Formatting

        public static String GetFirstName(String FullName, String Separator, Boolean Debug)
        {
            System.Diagnostics.EventLog.WriteEntry("MappingTools", "Executing: FullName = " + FullName + ", Separator = " + Separator,
                System.Diagnostics.EventLogEntryType.SuccessAudit);
            if (String.IsNullOrEmpty(Separator))
            {
                String ErrorMessage = "Separator can not be null or empty";
                System.Diagnostics.Trace.WriteLineIf(Debug,
                    DebugMessageFactory.GenerateTraceMessage(
                        System.Reflection.Assembly.GetExecutingAssembly().FullName,
                        System.Reflection.Assembly.GetExecutingAssembly().HostContext.ToString(),
                        ErrorMessage,
                        System.Diagnostics.EventLogEntryType.Warning));
                return String.Empty;
            }
            else
            {
                if (String.IsNullOrEmpty(FullName))
                {
                    String ErrorMessage = "FullName provided is null or empty";
                    System.Diagnostics.Trace.WriteLineIf(Debug,
                        DebugMessageFactory.GenerateTraceMessage(
                            System.Reflection.Assembly.GetExecutingAssembly().FullName,
                            System.Reflection.Assembly.GetExecutingAssembly().HostContext.ToString(),
                            ErrorMessage,
                            System.Diagnostics.EventLogEntryType.Warning));
                    return String.Empty;
                }
                else
                {
                    String[] NameParts = FullName.Split(Separator.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                    if (NameParts.Length > 0)
                    {
                        return NameParts[0];
                    }
                    else
                    {
                        return String.Empty;
                    }
                }
            }
        }

        public static String GetLastName(String FullName, String Separator, Boolean Debug)
        {
            if (String.IsNullOrEmpty(Separator))
            {
                String ErrorMessage = "Separator can not be null or empty";
                System.Diagnostics.Trace.WriteLineIf(Debug,
                    DebugMessageFactory.GenerateTraceMessage(
                        System.Reflection.Assembly.GetExecutingAssembly().FullName,
                        System.Reflection.Assembly.GetExecutingAssembly().HostContext.ToString(),
                        ErrorMessage,
                        System.Diagnostics.EventLogEntryType.Warning));
                return String.Empty;
            }
            else
            {
                if (String.IsNullOrEmpty(FullName))
                {
                    String ErrorMessage = "FullName provided is null or empty";
                    System.Diagnostics.Trace.WriteLineIf(Debug,
                        DebugMessageFactory.GenerateTraceMessage(
                            System.Reflection.Assembly.GetExecutingAssembly().FullName,
                            System.Reflection.Assembly.GetExecutingAssembly().HostContext.ToString(),
                            ErrorMessage,
                            System.Diagnostics.EventLogEntryType.Warning));
                    return String.Empty;
                }
                else
                {
                    String[] NameParts = FullName.Split(Separator.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                    if (NameParts.Length == 2)
                    {
                        return NameParts[1];
                    }
                    else if (NameParts.Length > 2)
                    {
                        return NameParts[NameParts.Length - 1];
                    }
                    else
                    {
                        return String.Empty;
                    }
                }
            }
        }

        #endregion

        #region Address & Phone Number Formatting

        public static String CleanAddress(String Address, String CharactersToRemove, Boolean Debug)
        {
            String ConfigKey = "InvalidAddressCharacters";
            String GoodAddress;
                if (String.IsNullOrEmpty(CharactersToRemove))
                {
                    String ErrorMessage = "No characters were specified to remove";
                    System.Diagnostics.Trace.WriteLineIf(Debug,
                        DebugMessageFactory.GenerateTraceMessage(
                            System.Reflection.Assembly.GetExecutingAssembly().FullName,
                            System.Reflection.Assembly.GetExecutingAssembly().HostContext.ToString(),
                            ErrorMessage,
                            System.Diagnostics.EventLogEntryType.Error));
                    return Address;
                }
                else
                {
                    GoodAddress = Address;
                    String[] s = CharactersToRemove.Replace(" ", String.Empty)
                        .Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                    foreach (String InvalidCharacter in s)
                    {
                        GoodAddress = GoodAddress.Replace(InvalidCharacter, String.Empty);
                    }
                    return GoodAddress;
                }
        }

        public static String FormatPhoneNumber_NumbersOnly(String AreaCode, String PhoneNumber, Boolean Debug)
        {
            if (String.IsNullOrEmpty(AreaCode) || String.IsNullOrEmpty(PhoneNumber))
            {
                String ErrorMessage = "Area Code and Phone Number can not be null or empty";
                System.Diagnostics.Trace.WriteLineIf(Debug,
                    DebugMessageFactory.GenerateTraceMessage(
                        System.Reflection.Assembly.GetExecutingAssembly().FullName,
                        System.Reflection.Assembly.GetExecutingAssembly().HostContext.ToString(),
                        ErrorMessage,
                        System.Diagnostics.EventLogEntryType.Warning));
                return String.Empty;
            }
            else
            {
                return AreaCode + PhoneNumber.Replace("-", String.Empty);
            }
        }

        public static String FormatPhoneNumber(String AreaCode, String PhoneNumber, Boolean Debug)
        {
            if (String.IsNullOrEmpty(AreaCode) || String.IsNullOrEmpty(PhoneNumber))
            {
                String ErrorMessage = "Area Code and Phone Number can not be null or empty";
                System.Diagnostics.Trace.WriteLineIf(Debug,
                    DebugMessageFactory.GenerateTraceMessage(
                        System.Reflection.Assembly.GetExecutingAssembly().FullName,
                        System.Reflection.Assembly.GetExecutingAssembly().HostContext.ToString(),
                        ErrorMessage,
                        System.Diagnostics.EventLogEntryType.Warning));
                return String.Empty;
            }
            else
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("(");
                sb.Append(AreaCode);
                sb.Append(")");
                sb.Append(PhoneNumber.Substring(0, 3));
                sb.Append("-");
                sb.Append(PhoneNumber.Substring(3));
                return sb.ToString();
            }
        }

        public static String FormatPhoneNumber(String AreaCode, String PhoneNumber, String Extension, Boolean Debug)
        {
            if (String.IsNullOrEmpty(AreaCode) || String.IsNullOrEmpty(PhoneNumber) || String.IsNullOrEmpty(Extension))
            {
                String ErrorMessage = "Area Code and Phone Number and Extension can not be null or empty";
                System.Diagnostics.Trace.WriteLineIf(Debug,
                    DebugMessageFactory.GenerateTraceMessage(
                        System.Reflection.Assembly.GetExecutingAssembly().FullName,
                        System.Reflection.Assembly.GetExecutingAssembly().HostContext.ToString(),
                        ErrorMessage,
                        System.Diagnostics.EventLogEntryType.Warning));
                return String.Empty;
            }
            else
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("(");
                sb.Append(AreaCode);
                sb.Append(")");
                sb.Append(PhoneNumber.Substring(0, 3));
                sb.Append("-");
                sb.Append(PhoneNumber.Substring(3));
                sb.Append(" ext ");
                sb.Append(Extension);
                return sb.ToString();
            }
        }

        #endregion

        #region Case Formatting

        public static String UseCamelCase(String Input, Boolean Debug)
        {
            if (String.IsNullOrEmpty(Input))
            {
                String ErrorMessage = "Input string was be null or empty";
                System.Diagnostics.Trace.WriteLineIf(Debug,
                    DebugMessageFactory.GenerateTraceMessage(
                        System.Reflection.Assembly.GetExecutingAssembly().FullName,
                        System.Reflection.Assembly.GetExecutingAssembly().HostContext.ToString(),
                        ErrorMessage,
                        System.Diagnostics.EventLogEntryType.Warning));
                return Input;
            }
            else
            {
                StringBuilder CamelCasedValue = new StringBuilder();
                foreach (String word in Input.Split(" ".ToCharArray(), StringSplitOptions.None))
                {
                    CamelCasedValue.Append(word.Substring(0, 1).ToUpper());
                    CamelCasedValue.Append(word.Substring(1, word.Length - 1).ToLower());
                    CamelCasedValue.Append(" ");
                }
                return CamelCasedValue.ToString();
            }
        }

        #endregion
    }
}
